﻿namespace WebCamera
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Text.RegularExpressions;

    public class BrowserPageFactory
    {
        public static BrowserPageProxy GetActivePage()
        {
            IntPtr focusControlHandle = GetFocusControlHandle();
            //Console.WriteLine(focusControlHandle);
            if (focusControlHandle != IntPtr.Zero)
            {
                WindowEumerator eumerator = new WindowEumerator("Windows Internet Explorer");
                eumerator.Search();
                IntPtr win = eumerator.win;
                BrowserWindow window = new BrowserWindow(win, BrowserType.IE7);
                window.Edge = Configration.IE7Edge;
                foreach (IntPtr ptr3 in IEPage.GetPageHandlers(win))
                {
                    if (OSSupport.IsWindowVisible(ptr3))
                    {
                        window.FocusControlHandle = focusControlHandle;
                        return new BrowserPageProxy(new IEPage(ptr3, window));
                    }
                }
            }
            else
            {
                foreach (BrowserPageProxy proxy in GetAllPages())
                {
                    if (OSSupport.IsWindowVisible(proxy.BrowserWindow.Handle) && OSSupport.IsWindowVisible(proxy.Handle))
                    {
                        return proxy;
                    }
                }
            }
            return null;
        }

        public static List<BrowserPageProxy> GetAllPages()
        {
            BrowserWindow window;
            BrowserPageProxy proxy;
            List<BrowserPageProxy> list = new List<BrowserPageProxy>();
            foreach (IntPtr ptr in GetMainWindowHwnd("Microsoft Internet Explorer"))
            {
                window = new BrowserWindow(ptr, BrowserType.IE6);
                window.Edge = Configration.IE6Edge;
                foreach (IntPtr ptr2 in IEPage.GetPageHandlers(ptr))
                {
                    IEPage page = new IEPage(ptr2, window);
                    proxy = new BrowserPageProxy(page);
                    list.Add(proxy);
                }
            }
            foreach (IntPtr ptr in GetMainWindowHwnd("Windows Internet Explorer"))
            {
                //Console.WriteLine(ptr);
                window = new BrowserWindow(ptr, BrowserType.IE7);
                window.Edge = Configration.IE7Edge;
                foreach (IntPtr ptr2 in IEPage.GetPageHandlers(ptr))
                {
                    IEPage page2 = new IEPage(ptr2, window);
                    proxy = new BrowserPageProxy(page2);
                    list.Add(proxy);
                }
            }
            foreach (IntPtr ptr in GetMainWindowHwnd("Mozilla Firefox"))
            {
                window = new BrowserWindow(ptr, BrowserType.FF3);
                window.Edge = Configration.FF3Edge;
                foreach (IntPtr ptr2 in FFPage.GetPageHandlers(ptr))
                {
                    FFPage page3 = new FFPage(ptr2, window);
                    proxy = new BrowserPageProxy(page3);
                    list.Add(proxy);
                }
            }
            return list;
        }

        public static List<BrowserPageProxy> GetAllPages(IntPtr win)
        {
            return new List<BrowserPageProxy>();
        }

        public static List<BrowserPageProxy> GetAllPages(string url)
        {
            List<BrowserPageProxy> list = new List<BrowserPageProxy>();
            foreach (BrowserPageProxy proxy in GetAllPages())
            {
                if (proxy.Url == url)
                {
                    list.Add(proxy);
                }
            }
            return list;
        }

        private static IntPtr GetFocusControlHandle()
        {
            Process[] processes = Process.GetProcesses();
            foreach (Process process in processes)
            {
                if ((process.ProcessName.Trim() == "iexplore") && (process.MainWindowTitle.Trim() == string.Empty))
                {
                    //Console.WriteLine(process.ProcessName);
                    return process.MainWindowHandle;
                }
            }
            return IntPtr.Zero;
        }

        private static List<IntPtr> GetMainWindowHwnd(string title)
        {
            Regex regex = new Regex(@"\w*" + title + @"\w*");
            return GetMainWindowHwnd(regex);
        }

        private static List<IntPtr> GetMainWindowHwnd(Regex regex)
        {
            List<IntPtr> list = new List<IntPtr>();
            Process[] processes = Process.GetProcesses();
            foreach (Process process in processes)
            {
                if (regex.Match(process.MainWindowTitle).Success)
                {
                    list.Add(process.MainWindowHandle);
                }
            }
            return list;
        }
    }
}

